package com.zjhcsoft.auth.controller;

import com.dc.eai.data.CompositeData;
import com.dc.eai.data.Field;
import com.dcfs.esb.utils.TLSimRepTools;
import com.zjhcsoft.auth.service.SmsService;
import io.swagger.annotations.ApiOperation;
import java.io.BufferedInputStream;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 认证
 * @Author: wzya
 * @Date: 2021/3/26
 */
@RestController
@RequestMapping("/sms")
@Slf4j
public class AuthController {

    @Autowired
    SmsService smsService;
    /**
     * 获取短信验证码
     */
    @PostMapping("/get/msg")
    @ApiOperation(value="获取短信验证码")
    public String getSmsCode(String mobile){
        return smsService.getSmsMsg(mobile);
    }

    /**
     * 短信验证码校验
     */
    @PostMapping("/check/msg")
    @ApiOperation(value="短信验证码校验")
    public Boolean checkSmsCode(String mobile,String msg){
        return smsService.smsMsgValidate(mobile,msg);
    }

    @PostMapping("/demo")
    public String demo(HttpServletRequest request, HttpServletResponse response) {
        try {
            BufferedInputStream is = new BufferedInputStream(request.getInputStream());
            CompositeData reqCD = TLSimRepTools.xmlToCD(IOUtils.toByteArray(is), "UTF-8");

            CompositeData sysHead = reqCD.getStruct("SYS_HEAD");
            CompositeData appHead = reqCD.getStruct("APP_HEAD");//ESB报文应用头
            CompositeData body = reqCD.getStruct("BODY");//ESB报文体

            Field field_code = sysHead.getField("SERVICE_CODE");
            Field field_scene = sysHead.getField("SERVICE_SCENE");

            //ESB服务码
            String serviceCode = field_code.strValue();
            //ESB场景码
            String serviceScene = field_scene.strValue();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "OK";
    }
}
